﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input;

namespace InkRun
{
    class PlayerFactory
    {
        private static PlayerFactory instance = null;


        List<Player> players = new List<Player>();
        public List<Player> Players
        {
            get { return players; }
            set { players = value; }
        }

        public List<Player> GetPlayers()
        {
            return players;
        }


        public static PlayerFactory Instance()
        {
            if (instance == null)
            {
                instance = new PlayerFactory();
            }
            return instance;
        }

        private PlayerFactory()
        {

        }

        public void Update(GameTime gameTime, Tiler tiler)
        {


            for (int i = 0; i < players.Count; i++)
            {
                players[i].Update(gameTime);
                if (players[i].playerState == Player.PlayerState.Alive)
                {
                    CheckCollisions(players[i]);
                    UpdateWhatTilePlayerIsOn(players[i], tiler);
                }
            }
            //players.RemoveAll(FindDeadPlayers);

            // Check if all the players are completely dead
            if (players.All(p => p.NoLivesLeft))
            {
                Console.WriteLine("ALL DEAD");
            }
        }
        public void UpdateWhatTilePlayerIsOn(Player p, Tiler t)
        {
            if (p.tileCurrentlyOn == null)
            {
                p.tileCurrentlyOn = t.tiles[0];
            }

            if (!p.CheckForCollision(p.tileCurrentlyOn))
            {
                for (int i = 0; i < t.tiles.Count(); i++)
                {
                    if (p.CheckForCollision(t.tiles[i]))
                    {
                        p.tileCurrentlyOn = t.tiles[i];
                        return;
                    }
                }
            }
        }
        private bool FindDeadPlayers(Player p)
        {
            if (!p.Alive)
            {
                return true;
            }
            return false;
        }
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach(Player player in players)
            {
                player.Draw(gameTime, spriteBatch);                
            }
        }

        public void LoadContent(ContentManager content)
        {
            Player.PlayerSprite1 = Player.LoadContent(content, "Sprites/BlueInkChar");
            Player.PlayerSprite2 = Player.LoadContent(content, "Sprites/RedInkChar");
        }

        public void CreatePlayers()
        {

            players.Add(new Player(1, GameParameters.PLAYER1_START_POSITION));
            players.Add(new Player(2, GameParameters.PLAYER2_START_POSITION));
        }
        public Player GetFurthestForwardPlayer()
        {
            Player farthestPlayer = new Player(new Vector2(0,0));
            foreach (Player p in players)
            {
                if (p.position.X > farthestPlayer.position.X)
                {
                    farthestPlayer = p;
                }
            }
            return farthestPlayer;
        }
        public void CreatePlayer(int playerID)
        {
            if (playerID == 1)
            {
                players.Add(new Player(playerID, GameParameters.PLAYER1_START_POSITION));
            }
            else
            {
                players.Add(new Player(playerID, GameParameters.PLAYER2_START_POSITION));
            }
        }
        public void CheckCollisions(Player player)
        {
            for (int e = 0; e < players.Count; e++)
            {
                    if( players[e] != player && player.CheckForCollision(players[e]) && player.velocity != players[e].velocity)
                    {
                        Vector2 P1 = new Vector2(player.velocity.X, player.velocity.Y);
                        Vector2 P2 = new Vector2(players[e].velocity.X, players[e].velocity.Y);
                        player.velocity = Vector2.Multiply(P2, 0.75f);
                        players[e].velocity = Vector2.Multiply(P1, 0.75f);
                    }
                
            }
        }
    }
}
